/***********************************************
 * Title:       SubjectService.java
 * Description: SubjectService.java
 * Author:      Niyuzhe
 * Create Date: 2010-1-23
 * CopyRight:   CopyRight(c)@2009
 * Company:     TJUSCS
 * Version:     1.0
 ***********************************************
 */
package edu.tju.education.model;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.younker.waf.db.IBatisConfig;

/**
 * @author Niyuzhe
 *
 */
public class SubjectService implements ISubjectService
{
        private SqlMapClient sqlMap = IBatisConfig.getSqlMapInstance();
        private Logger log = Logger.getLogger(this.getClass());

        /* (non-Javadoc)
         * @see edu.tju.education.model.ISubjectService#getSubject(long)
         */
        public Subject getSubjectById(long subjectId)
        {
                Subject subject = null;
                try
                {
                        subject = (Subject) sqlMap.queryForObject("getSubjectById", subjectId);
                } catch (SQLException e)
                {
                        log.error("get subject error[id=" + subjectId + "].", e );
                        subject = null;
                }
                return subject;
        }

        /* (non-Javadoc)
         * @see edu.tju.education.model.ISubjectService#insertSubject(edu.tju.education.model.Subject)
         */
       
        public long insertSubject(Subject subject)
        {
                Long ret;
                try
                {
                        ret = (Long)sqlMap.insert("insertSubject", subject);
                } catch (SQLException e)
                {
                        log.error("Insert subject error", e );
                        ret = new Long(-1);
                }
                return ret;
        }

        /* (non-Javadoc)
         * @see edu.tju.education.model.ISubjectService#updateSubject(edu.tju.education.model.Subject)
         */
   
        public long updateSubject(Subject subject)
        {
                try
                {
                        sqlMap.update("updateSubject", subject);
                        return 0;
                } catch (SQLException e)
                {
                        log.error("Update subject error", e);
                        return -1;
                }
        }

        /* (non-Javadoc)
         * @see edu.tju.education.model.ISubjectService#setTeacherSubject(long, long)
         */
       
        public long setTeacherSubject(long subjectId, long teacherId)
        {
                Map<String, Long> map = new HashMap<String, Long>();
                map.put("teacherId", teacherId);
                map.put("subjectId", subjectId);
                Long retVal;
                try
                {
                        retVal = (Long) sqlMap.insert("setTeacherSubject", map);
                } catch (SQLException e)
                {
                        log.error("setTeacherSubject error:", e);
                        retVal = new Long(-1);
                }
                
                return retVal;
        }

        /* (non-Javadoc)
         * @see edu.tju.education.model.ISubjectService#unsetTeacherSubject(long, long)
         */
       
        public long unsetTeacherSubject(long teacherSubjectId)
        {
                Long retVal;
                try
                {
                        sqlMap.delete("unsetTeacherSubject", teacherSubjectId);
                        retVal = new Long(1);
                } catch (SQLException e)
                {
                        log.error("unsetTeacherSubject error:", e);
                        retVal = new Long(-1);
                }
                
                return retVal;
        }

        /* (non-Javadoc)
         * @see edu.tju.education.model.ISubjectService#setSpecialitySubject(long, long, java.lang.String, java.lang.String)
         */
       
        public int setSpecialitySubject(long subjectId, long specialityId, String studyTerm,
                        String studyYear)
        {
                CoursePlan coursePlan = new CoursePlan();
                coursePlan.setSubjectId(subjectId);
                coursePlan.setSpecialityId(specialityId);
                coursePlan.setStudyTerm(studyTerm);
                coursePlan.setStudyYear(studyYear);
                
                try
                {
                        sqlMap.insert("insertCoursePlan", coursePlan);
                } catch (SQLException e)
                {
                        return ISubjectService.RV_DBERROR;
                }
                return ISubjectService.RV_SUCCESS;
        }

}
